package com.example.administrator.testc.DBs;/**
 * Created by Administrator on 2016/6/6.
 */

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.Button;

import com.example.administrator.testc.R;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>类描述
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>属性描述
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>方法描述
 */
public class TestMain extends AppCompatActivity {

    private String testStr =
            "{\n" +
                    "    \"msg\": \"操作成功\", //返回消息\n" +
                    "    \"result\": {\n" +
                    "        \"clickUrl\": \"http://m.xincheping.com/\", //点击的url\n" +
                    "        \"file\": \"http://file.xincheping.com/201606/89/a4ec7081bd4752914822b8d3d4d841.jpg\", //素材的url\n" +
                    "        \"positionId\": 256, //位置id\n" +
                    "        \"width\": 1080, //素材宽度\n" +
                    "        \"id\": 2199, //广告id\n" +
                    "        \"pubDates\": \"2016-06-01,2016-06-02,2016-06-03,2016-06-04,2016-06-05,2016-06-06\", //发布日期\n" +
                    "        \"pageId\": 25, //位置所在页面id\n" +
                    "        \"height\": 1476 //素材高度\n" +
                    "    }, \n" +
                    "    \"code\": 1 //返回码 1正常,-1出错或者没有广告\n" +
                    "}";

    private String testStr2 =
            "{\n" +
                    "    \"msg\": \"操作成功\", //返回消息\n" +
                    "    \"result\": {\n" +
                    "        \"clickUrl\": \"https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&tn=baidu&wd=%E5%9B%BE%E7%89%87&oq=android%20SqliteOpenhelper%E6%95%B0%E6%8D%AE%E5%BA%93%E8%B7%AF%E5%BE%84&rsv_pq=b681437900004b09&rsv_t=e25cKkJC3jJ0C6tUXxpwn10HGWpUiKB59YEekzGdySkWz7rT5AMUbMx1%2Bl0&rqlang=cn&rsv_enter=1&rsv_sug3=4&rsv_sug1=4&rsv_sug7=100&rsv_sug2=0&inputT=1257&rsv_sug4=1257\", //点击的url\n" +
                    "        \"file\": \"http://pic1.sc.chinaz.com/Files/pic/pic9/201605/apic20592_s.jpg\", //素材的url\n" +
                    "        \"positionId\": 256, //位置id\n" +
                    "        \"width\": 1080, //素材宽度\n" +
                    "        \"id\": 2199, //广告id\n" +
                    "        \"pubDates\": \"2016-06-01,2016-06-02,2016-06-03,2016-06-04,2016-06-05,2016-06-06\", //发布日期\n" +
                    "        \"pageId\": 25, //位置所在页面id\n" +
                    "        \"height\": 1476 //素材高度\n" +
                    "    }, \n" +
                    "    \"code\": 1 //返回码 1正常,-1出错或者没有广告\n" +
                    "}";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.layout_db);
        ServiceData.initDB();

        findViewById(R.id.update).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        String clickUlr = daoAdvertising.Query().getClickUrl();
                        Log.i("TAGSS", "clickUlr : " + clickUlr);
                        ((WebView) findViewById(R.id.mWebView)).loadUrl(clickUlr);
                    }
                });
            }
        });
        findViewById(R.id.save).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    JSONObject jsonObject = new JSONObject(testStr);
                    AdvertisingBean mData = new Gson().fromJson(jsonObject.getString("result"), AdvertisingBean.class);
                    daoAdvertising.Save(mData);
                    String url = daoAdvertising.Query().getFile();
                    new DownImg(url);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
